package com.decheng.mp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.decheng.mp.entity.User;
import com.decheng.mp.mapper.UserMapper;
import com.decheng.mp.result.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/2 6:06 下午
 * @desc
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserMapper userMapper;

    public UserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public ApiResult findAll() {
        return ApiResult.ok().data(userMapper.selectList(null));
    }

    @ApiOperation("根据id查询")
    @GetMapping("findById/{id}")
    public ApiResult findById(@PathVariable String id) {
        return ApiResult.ok().data("item", userMapper.selectById(id));
    }

    @ApiOperation("添加")
    @PostMapping("save")
    public ApiResult saveUser(@RequestBody User user) {
        return ApiResult.ok().data("result", userMapper.insert(user));
    }

    @ApiOperation("根据id修改")
    @PutMapping("updateById")
    public ApiResult updateById(@RequestBody User user) {
        return ApiResult.ok().data("result", userMapper.updateById(user));
    }

    @ApiOperation("根据id批量查询")
    @PostMapping("findBatchIds")
    public ApiResult findBatchIds(@RequestBody List<Integer> idList) {
        return ApiResult.ok().data(userMapper.selectBatchIds(idList));
    }

    @ApiOperation("根据姓名和年龄查询")
    @GetMapping("findByNameAndAge")
    public ApiResult findByMap(@RequestParam(required = false) String name,
                               @RequestParam(required = false) Integer age) {
        Map<String, Object> map = new HashMap<>(2);
        // key 为 table column
        if (StrUtil.isNotBlank(name)) {
            map.put("name", name);
        }
        if (ObjectUtil.isNotNull(age)) {
            map.put("age", 18);
        }
        // SELECT id,name,age,email,create_time,update_time,version FROM user WHERE name = ? AND age = ?
        return ApiResult.ok().data(userMapper.selectByMap(map));
    }

    @ApiOperation("分页查询")
    @GetMapping("findPage/{current}/{size}")
    public ApiResult findPage(@PathVariable long current, @PathVariable long size) {
        Page<User> page = new Page<>(current, size);
        // SELECT id,name,age,email,create_time,update_time,version FROM user LIMIT 0,5
        userMapper.selectPage(page, null);
        return ApiResult.ok().data(page.getTotal(), page.getRecords());
    }


    @ApiOperation("根据id删除")
    @DeleteMapping("deleteById/{id}")
    public ApiResult deleteById(@PathVariable Long id) {
        return ApiResult.ok().data("result", userMapper.deleteById(id));
    }

    @ApiOperation("根据id批量删除")
    @DeleteMapping("deleteBatchIds/")
    public ApiResult deleteBatchIds(@RequestBody List<Integer> idList) {
        return ApiResult.ok().data("result", userMapper.deleteBatchIds(idList));
    }

    @ApiOperation("根据姓名或年龄删除")
    @DeleteMapping("deleteByNameOrAge/")
    public ApiResult deleteByNameOrAge(@RequestParam(required = false) String name,
                                       @RequestParam(required = false) Integer age) {
        Map<String, Object> map = new HashMap<>(2);
        // key 为 table column
        if (StrUtil.isNotBlank(name)) {
            map.put("name", name);
        }
        if (ObjectUtil.isNotNull(age)) {
            map.put("age", 18);
        }
        return ApiResult.ok().data("result", userMapper.deleteByMap(map));
    }


    @ApiOperation("查询大于等于指定年龄的用户")
    @GetMapping("findByAge/ge/{age}")
    public ApiResult findByAgeWrapperGe(@PathVariable Integer age) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // ge、gt、le、lt、isNull、isNotNull
        userQueryWrapper.ge("age", age);
        return ApiResult.ok().data(userMapper.selectList(userQueryWrapper));
    }

    @ApiOperation("根据姓名查询一个用户")
    @GetMapping("findByName/eq/{name}")
    public ApiResult findByNameWrapperEq(@PathVariable String name) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // eq, ne
        userQueryWrapper.eq("name", name);
        return ApiResult.ok().data("item", userMapper.selectOne(userQueryWrapper));
    }


    @ApiOperation("统计年龄在某范围之间的用户")
    @GetMapping("countUserByAge")
    public ApiResult deleteBatchIds(@RequestParam Integer min, @RequestParam Integer max) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // between、notBetween 包含边界
        userQueryWrapper.between("age", min, max);
        // SELECT COUNT( 1 ) FROM user WHERE is_deleted=0 AND (age BETWEEN min AND max)
        return ApiResult.ok().data("result", userMapper.selectCount(userQueryWrapper));
    }


    @ApiOperation("QueryWrapper.allEq")
    @GetMapping("findUser/allEq")
    public ApiResult findUserWrapperAllEq(@RequestParam Long id, @RequestParam String name, @RequestParam Integer age) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>(3);
        map.put("id", id);
        map.put("name", name);
        map.put("age", age);
        // WHERE is_deleted=0 AND (name = Jack AND id = 2 AND age = 20)
        wrapper.allEq(map);
        return ApiResult.ok().data(userMapper.selectList(wrapper));
    }

    @ApiOperation("根据姓名和邮箱模糊查询用户")
    @GetMapping("findUser/notLike/{name}/likeRight/{email}")
    public ApiResult findUserWrapperLike(@PathVariable String name, @PathVariable String email) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // like、notLike、likeLeft、likeRight
        // WHERE is_deleted=0 AND (name NOT LIKE "%e%" AND email LIKE "t%")
        wrapper.notLike("name", name).likeRight("email", email);
        return ApiResult.ok().data(userMapper.selectMaps(wrapper));
    }
}
